/*
 * @Author: yangzonglong
 * @Date: 2021-02-25 14:35:42
 * @version: v1.0.0
 * @Descripttion: 
 * @LastEditors: yangzonglong
 * @LastEditTime: 2021-04-29 14:28:55
 * @Auditor: 
 */
import React, { useCallback } from 'react';
import { InputNumber } from 'antd';

const DEFAULT_MAX = 9999999999999;
interface IProps {
  id: string;
  value: number;
  onChange(value: number, id: string): void;
  min?: number;
  max?: number;
  integer?: boolean
}

export default (props: IProps) => {
  const { id, onChange, integer, ...options } = props;
  const _onChange = useCallback((e) => onChange(e, id), [id, onChange]);

  const formatter = useCallback((value) => {
    if(!integer || value === '0') return value;
    return String(value).replace(/^(0+)|[^\d]+/g, '')
  }, [integer]);
  
  return (
    <InputNumber 
      {...options} 
      max={options.max || DEFAULT_MAX}
      formatter={formatter} 
      parser={formatter} 
      onChange={_onChange}/>
  )
}